package thread;
/*

 */

public class JoinDemo {
    //标识图片是否下载完毕
    private static boolean isFinish = false;
    public static void main(String[] args) {
        Thread download = new Thread(){
            public void run(){
                System.out.println("down:开始下载图片...");
                for(int i=1;i<=100;i++){
                    System.out.println("down:"+i+"%");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                    }
                }
                System.out.println("down:图片下载完毕！");
                isFinish = true;
            }
        };

        Thread show = new Thread(){
            public void run(){
                System.out.println("开始显示商品列表...");
                System.out.println("显示列表中的文字...");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                }
                System.out.println("列表中的文字显示完毕");
                System.out.println("开始显示图片...");
                //等待download执行完毕
                try {
                    /*
                        show线程执行到这里是会进入阻塞状态，直到download结束
                        才会结束阻塞。
                     */
                    download.join();
                } catch (InterruptedException e) {
                }

                if(!isFinish){ //如果图片没有下载完毕
                    throw new RuntimeException("图片加载失败！");
                }
                System.out.println("显示图片完毕！");
            }
        };
        download.start();
        show.start();









    }
}
